package com.sunjob.mroassets.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.sunjob.common.domain.assets.Instance;
import com.sunjob.common.mroAnno.ControllerLog;
import com.sunjob.common.mroAnno.LogModuleType;
import com.sunjob.common.result.Result;
import com.sunjob.mroassets.server.StatusService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@Api(tags = "实体状态信息服务接口")
public class StatusController {

    @Autowired
    StatusService statusService;

     /**
     * 查询status状态
     **/
    @CrossOrigin
    @ApiOperation("查询status状态接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "状态名称", name = "status"),
            @ApiImplicitParam(value = "状态类型", name = "Type")
    })//描述入参信息
    @ApiResponses({
            @ApiResponse(code = 404, message = "无此路径"),
            @ApiResponse(code = 500, message = "服务内部错误"),
            @ApiResponse(code = 200, message = "访问成功")
    })
    @RequestMapping("/getTypeByStatus")
    public Result getListByType(@RequestParam("status") Integer status, @RequestParam("type") String type) {
        Result result = new Result();
        
        result.setData(statusService.getListByTypeAndStatus(type,status));
        if (result.getData() == null) {
            result.setCode(500);
        } else {
            result.setCode(200);
        }

        return result;
    }


    @ApiOperation("修改实例状态为故障接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "实例id",name = "instanceId")
    })//描述入参信息
    @ApiResponses({
            @ApiResponse(code = 404,message = "无此路径"),
            @ApiResponse(code = 500,message = "服务内部错误"),
            @ApiResponse(code = 200,message = "访问成功")
    })
    @ControllerLog(value = LogModuleType.ASSETS)
    //修改实例状态为故障，传入故障实例的id
    @RequestMapping("instanceStatusRepair")
    public void instanceStatusRepair(@RequestParam("instanceId") Long instanceId){
        Result result = new Result();
        //调用服务层修改实例状态
        int flag = statusService.updateInstanceRepair(instanceId);

    }
    @ApiOperation("修改实例状态为正常接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "实例",name = "instance")
    })//描述入参信息
    @ApiResponses({
            @ApiResponse(code = 404,message = "无此路径"),
            @ApiResponse(code = 500,message = "服务内部错误"),
            @ApiResponse(code = 200,message = "访问成功")
    })
    @ControllerLog(value = LogModuleType.ASSETS)
    //修改实例状态为正常，传入故障实例的对象
    @RequestMapping("instanceStatusNormal")
    public Result instanceStatusNormal(@RequestBody Instance instance){
        Result result = new Result();
        System.out.println(instance);
        //调用服务层修改实例状态
        int flag = statusService.updateInstanceNormal(instance);
        return result;
    }
    @ApiOperation("修改实例状态为正常，按照部件名称修改使用部件寿命接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "实例",name = "instance")
    })//描述入参信息
    @ApiResponses({
            @ApiResponse(code = 404,message = "无此路径"),
            @ApiResponse(code = 500,message = "服务内部错误"),
            @ApiResponse(code = 200,message = "访问成功")
    })
    @ControllerLog(value = LogModuleType.ASSETS)
    //修改实例状态为正常，传入故障实例的对象
    @RequestMapping("instanceStatusNormalByPartName")
    public Result instanceStatusNormalByPartName(@RequestParam("instance") Instance instance){
        Result result = new Result();
        //调用服务层修改实例状态
        int flag = statusService.updateInstanceNormalByPartName(instance);
        return result;
    }
    @ApiOperation("修改实例状态为报废接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "实例id",name = "instanceId")
    })//描述入参信息
    @ApiResponses({
            @ApiResponse(code = 404,message = "无此路径"),
            @ApiResponse(code = 500,message = "服务内部错误"),
            @ApiResponse(code = 200,message = "访问成功")
    })
    @ControllerLog(value = LogModuleType.ASSETS)
    //修改实例状态为报废，传入故障实例的id
    @RequestMapping("instanceStatusScrap")
    public void instanceStatusScrap(@RequestParam("instanceId") Long instanceId){
        Result result = new Result();
        //调用服务层修改实例状态
        int flag = statusService.updateInstanceScrap(instanceId);
    }
    //定时修改数据库中实体的状态
    //触发时间为每天的上午九点
    @Scheduled(cron = "0 0 9 * * ?")
    public void statusTask(){
        statusService.updateEntityStatus();
    }
}
